package com.wandian.games.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mylib.libcore.tools.CSJADTools;
import com.mylib.libcore.utils.ScreenUtils;
import com.wandian.games.R;

import static com.mylib.libcore.config.AppConfig.CSJ_BANNER_ONE_ID;


/**
 * 奖品弹框
 */
public class LotteryPrizeDialog extends PopupWindow {

    private View mMenuView;
    public DialogViewClink dialogViewClink;

    public interface  DialogViewClink{
        void viewClink(boolean isSign);
    }

    public LotteryPrizeDialog(int width, String goldcoin, Activity context) {
        super(context);


        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_lottery_prize, null);
        TextView tv_lottery_name = (TextView) mMenuView.findViewById(R.id.tv_coin_num);
        TextView tv_btn = (TextView) mMenuView.findViewById(R.id.tv_btn);
        tv_lottery_name.setText(""+goldcoin);
        RelativeLayout rl_ad = (RelativeLayout)mMenuView.findViewById(R.id.rl_ad);
        CSJADTools.loadCSJAdMiddleBanner(context,rl_ad,"",CSJ_BANNER_ONE_ID,width,90);


        ImageView iv_close = (ImageView) mMenuView.findViewById(R.id.iv_close);
        tv_btn.setOnClickListener(v -> dismiss());
        iv_close.setOnClickListener(v -> dismiss());

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setClippingEnabled(false);
        }


        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ScreenUtils.getScreenWidth(context));
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ScreenUtils.getScreenHeight(context)+ ScreenUtils.getScreenHeight(context)+200);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
//        //设置SelectPicPopupWindow弹出窗体动画效果-默认中间
       // this.setClippingEnabled(false);
//        this.setAnimationStyle(R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(mContext.getResources().getColor(R.color.transparent));
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框


    }



    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

}